package com.ruge.user.controller;

import com.ruge.core.response.Response;
import com.ruge.user.domain.request.ListUserRequest;
import com.ruge.user.domain.request.SaveUserRequest;
import com.ruge.user.domain.vo.UserVo;
import com.ruge.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * @author ruge.wu
 * @since 2021/12/14 12:40
 */
@Slf4j
@Validated
@RestController
@RequestMapping("public/user")
@Api(tags = "【用户 相关 接口】", value = "【用户 相关 接口】")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private RedisTokenStore redisTokenStore;


    @GetMapping("checkPhoneIsRegister")
    @ApiOperation(value = "【用户】  校验手机号是否已注册")
    public Response checkPhoneIsRegister(@ApiParam(name = "mobile", value = "手机号", required = true) @NotBlank(message = "手机号不能为空") @RequestParam("mobile") String mobile) {
        return userService.checkPhoneIsRegister(mobile);
    }

    @GetMapping("getUserInfo")
    @ApiOperation(value = "【用户】  获取当前登录用户信息", notes = "http://localhost:8085/getUserInfo?access_token=58c2ba85-9930-49dc-a477-264e9907d6ff")
    public Response getUserInfo(Authentication authentication) {
        Object principal = authentication.getPrincipal();
        return Response.create(principal);
    }

    @GetMapping("logout")
    @ApiOperation(value = "【用户】  登出", notes = "")
    public Response logout(String access_token, String authorization) {
        // 判断 access_token 是否为空，为空将 authorization 赋值给 access_token
        if (StringUtils.isBlank(access_token)) {
            access_token = authorization;
        }
        // 判断 authorization 是否为空
        if (StringUtils.isBlank(access_token)) {
            return Response.create("退出成功");
        }
        // 判断 bearer token 是否为空
        if (access_token.toLowerCase().contains("bearer ".toLowerCase())) {
            access_token = access_token.toLowerCase().replace("bearer ", "");
        }
        // 清除 redis token 信息
        OAuth2AccessToken oAuth2AccessToken = redisTokenStore.readAccessToken(access_token);
        if (oAuth2AccessToken != null) {
            redisTokenStore.removeAccessToken(oAuth2AccessToken);
            OAuth2RefreshToken refreshToken = oAuth2AccessToken.getRefreshToken();
            redisTokenStore.removeRefreshToken(refreshToken);
        }
        return Response.create("退出成功");
    }

    @PostMapping("save")
    @ApiOperation(value = "【用户】  init100条数据")
    public Response save(@RequestBody SaveUserRequest request) {
        return userService.save(request);
    }

    @PostMapping("list")
    @ApiOperation(value = "【用户】  查询用户数据")
    public Response<UserVo> list(@RequestBody ListUserRequest request) {
        return userService.list(request);
    }
}

